#include<stdio.h>
#include<malloc.h>
#include<stdlib.h> 
  

   struct node                            //Structure containing data and next part..
 {
   int data;
   struct node *next;
 }*p;



  

   void pop(int num)
 {
     struct node *temp, *m;
temp=p;
      while(temp!=NULL)
    {
       if(temp->data==num)
       {
           if(temp==p)
           {
              p=temp->next;
              free(temp);
              return;
           }
           else
         {
           m->next=temp->next;
           free(temp);
           return;
         }
      }else
        {
           m=temp;
          temp= temp->next;
        }

}
    printf("\n ELEMENT %d NOT FOUND ", num);
} 

 void append( int num )
 {
     struct node *temp,*r;
     
       temp= (struct node *)malloc(sizeof(struct node));
       temp->data=num;
       r=(struct node *)p;

      if (p == NULL) 
     {
    p=temp;
         p->next =NULL;
     }
  else
     {        

       while( r->next != NULL)
       r=r->next;
       r->next =temp;
       r=temp;
       r->next=NULL;
     }
  }

      /* addbeg( int num )
   {
        /*  CREATING A NODE AND INSERTING VALUE TO IT  */

   /*struct node *temp;
        temp=(struct node *)malloc(sizeof(struct node));
        temp->data=num;

       /* IF LIST IS NULL ADD AT BEGINNING  */
     /*   if ( p== NULL)
       {
          p=temp;
          p->next=NULL;
       }

   else
      {
          temp->next=p;
          p=temp;
      }
   }*/



 insert(int loc, int num)
  {
     int i;
     struct node *temp,*t,*r;
     r=p;       
      if(loc > count()+1 || loc < 0)
   {
         printf("insertion is not possible :");
            return;
   }
       else if (loc == 0)
         {
           //addbeg(num);
           temp=(struct node *)malloc(sizeof(struct node));
           temp->data=num;     

       
            if(p==NULL)
         {
          p=temp;
          p->next=NULL;
         }

            else
         {
          temp->next=p;
          p=temp;
}
       return;
         }
      else
 {
       for(i=0;i<loc;i++)
          {
             t=r;  
             r=r->next;
          }
         temp=(struct node *)malloc(sizeof(struct node));
         temp->data=num;
         t->next=temp;
         t=temp;
         t->next=r;
        return;
       }
}

  display(struct node *r)
  {
      r=p;
      if(r==NULL)
     {
       printf("[]");
       return;
     }
      printf("["); 

       while(r!=NULL)
    {
      printf("%d ",r->data);
      r=r->next;
    }
     printf("]");
  }

count()
 {
   struct node *n;
   int c=0;
   n=p;
    while(n!=NULL)
   {
     n=n->next;
     c++;
   }
  return(c);
 }
//FUNCTION TO REVERSE THE LIST
reverse(struct node *q)
{
   struct node *m, *n,*l,*s;
   m=q;
   n=NULL;
  while(m!=NULL)
 {
   s=n;
   n=m;
  m=m->next;
  n->next=s;
 }
   p=n;
}


/*  MAIN FUNCTION  */

  main()
 {
        int i,num;
   p=NULL;
   struct node *n;
   do
 {
    
    
    printf("\n 1.APPEND AN ELEMENT INTO THE LIST:");
    printf("\n 2.INSERT A NUMBER AT A PARTICULAR LOCATION IN THE LIST:");
    printf("\n 3.PRINT THE LIST :");
    printf("\n 4.PRINT THE NUMBER OF ELEMENTS IN THE LIST  ");
    printf("\n 5.POP AN ELEMENT FROM THE LIST:");
    printf("\n 6.REVERSE THE LIST :");
    printf("\n 7.EXIT:");
    printf("\n PLEASE, ENTER YOUR CHOICE:");
    scanf("%d",&i);

      switch(i)
    {
         
          case 1:
       {
         int num;
         printf("\n PLEASE ENTER THE NUMBER :-");
         scanf("%d",&num);
         append(num);
         break;
       }

   case 2:
     {
      int x, i,k;
      printf("PLEASE ENTER THE NUMBER :-");
      scanf("%d",&x);
      printf("PLEASE ENTER THE LOCATION NUMBER :-");
      scanf("%d",&i);
      insert(i,x);
      break;
    }  case 3:
      {
         
         printf("\n THE  ELEMENTS IN THE LIST ARE : ");
         display(n);
         break;
      }

     case 4:
   {
      struct node *n;
      display(n);
      printf(" TOTAL NO OF ELEMENTS IN THE LIST ARE %d",count());
      break;
   }  case 5:
    {
      int num;
      printf("PLEASE ENTER A NUMBER FROM THE LIST :");
      scanf("%d",&num);
      pop(num);
     break;
    }
   case 6:
    {
      reverse(p);
        display(p);
        break;
 }
case 7:exit(0);
    }
 }while((i>=1)&&(i<=6));
}
